package com.scut.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scut.project.entity.Feedback;
import com.scut.project.entity.Interview;
import com.scut.project.entity.ShortAnswerQuestion;
import com.scut.project.mapper.FeedbackMapper;
import com.scut.project.mapper.InterviewMapper;
import com.scut.project.mapper.ShortAnswerQuestionMapper;
import com.scut.project.service.InterviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 面试流程记录 服务实现类
 * </p>
 *
 * @author Kevin Zheng
 * @since 2024-10-15
 */

import com.scut.project.entity.Answer;
import com.scut.project.mapper.AnswerMapper;
import com.scut.project.service.InterviewService;
import com.scut.project.client.LLMClient;
import com.scut.project.client.SpeechRecognitionClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class InterviewServiceImpl {

    @Autowired
    private ShortAnswerQuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private LLMClient llmClient; // 假设的语言模型客户端
    @Autowired
    private SpeechRecognitionClient speechClient; // 假设的语音识别客户端

    // 获取面试问题列表
    public List<ShortAnswerQuestion> getQuestions(Long interviewId) {
        return questionMapper.selectList(new QueryWrapper<ShortAnswerQuestion>().eq("interview_id", interviewId));
    }

    // 处理语音输入并转为文本
    public String processVoiceInput(InputStream voiceInput) {
        return speechClient.recognizeSpeech(voiceInput);
    }

    // 评估回答并保存反馈
    public Feedback evaluateAndSaveAnswer(Long questionId, String answerText, Long candidateId) {
        ShortAnswerQuestion question = questionMapper.selectById(questionId);
        String feedbackText = llmClient.getFeedback(question.getQuestionText(), answerText);
        int score = llmClient.getScore(answerText);

        // 保存答案
        Answer answer = new Answer();
        answer.setQuestionId(questionId);
        answer.setCandidateId(candidateId);
        answer.setAnswerText(answerText);
        answer.setUpdateTime(LocalDateTime.now());
        answerMapper.insert(answer);

        // 保存反馈
        Feedback feedback = new Feedback();
        feedback.setInterviewId(question.getInterviewId());
        feedback.setCandidateId(candidateId);
        feedback.setScore(score);
        feedback.setComments(feedbackText);
        feedbackMapper.insert(feedback);

        return feedback;
    }
}
